package cat.service.impl;

import cat.entity.Article;
import cat.entity.Comment;
import cat.mapper.ArticleMapper;
import cat.mapper.CommentMapper;
import cat.service.CommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;

    @Resource
    ArticleMapper articleMapper;

    //查询最新n条评论
    public List<Comment> listRecentComment(Integer n) {
        List<Comment> commentList = commentMapper.listRecentComment(n);
        for(int i=0;i<commentList.size();i++){
            //得到评论区信息中文章对应的id
            int commentArticleId = commentList.get(i).getCommentArticleId();
            Article article = articleMapper.selectArticleById(commentArticleId);
            commentList.get(i).setArticle(article);
        }
        return commentList;
    }

    //删除评论
    public void deleteComment(Integer CommentId) {
        commentMapper.deleteComment(CommentId);
    }
}
